package cn.tedu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/*
* JDBC快速入门程序
* 查询jt_db库中的account表中的所有记录
* select*from account;
* */
public class JdbcDome1 {
    public static void main(String[] args) throws Exception {
        //1.注册数据库驱动(可以省略但建议加上)
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取数据库连接
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3307/jt_db?characterEncoding=utf-8&serverTimezone=Asia/Shanghai", "root", "root");
        System.out.println(connection);
        //3.获取传输器
        Statement stat = connection.createStatement();

        //4.发送sql到数据库执行,并返回结果
        ResultSet result = stat.executeQuery("select*from account");

        while (result.next()){
            System.out.println(result.getInt("id"));
            System.out.println(result.getString("name"));
            System.out.println(result.getDouble("money"));
        }
        System.out.println(result);

        //5.处理结果(打印到控制台)
        //6.释放资源
        result.close();
        stat.close();
        connection.close();
    }
}
